#include <stdio.h>
#include <stdlib.h>
#include "ArrayG.h"

Graph CreateGraph(GraphType type) {
    Graph G = malloc(sizeof(GraphStruct));
    if (G == NULL) {
        fprintf(stderr, "Memory allocation failed for Graph.\n");
        exit(EXIT_FAILURE);
    }

    G->nodenum = 0;
    G->arcnum = 0;
    G->type=type;

    for (int i = 0; i < MAX_NODE_NUM; ++i) {
        for (int j = 0; j < MAX_NODE_NUM; ++j) {
            G->arcs[i][j].weight = INF;
            G->arcs[i][j].info = NULL;
        }
    }
    return G;
}

void freeGraph(Graph G) {
    if (G->info != NULL) {
        free(G->info);
    }
    free(G);
}

void add_arc(Graph G,char h,char t,float w){
    int i,j;
    for (i=0;i<G->nodenum;++i)
        if (G->nodes[i]==h) break;
    if (i==G->nodenum){
        G->nodes[i]=h;
        G->nodenum++;
        }
    for (j=0;j<G->nodenum;++j)
        if (G->nodes[j]==t) break;
    if (j==G->nodenum){
        G->nodes[j]=t;
        G->nodenum++;
        }
    G->arcs[i][j].weight=w;
}

void print_Graph(Graph G){
    int i,j;
    printf("\t");
    for (j = 0; j < G->nodenum; ++j)
        printf("%c\t",G->nodes[j]);
    printf("\n");
    for (i = 0;i < G->nodenum; ++i) {
        printf("%c\t",G->nodes[i]);
        for (j = 0; j < G->nodenum; ++j) {
            if (G->arcs[i][j].weight<INF)
                printf("%4.1f\t",G->arcs[i][j].weight);
            else
                printf("-\t");
        }
        printf("\n");
    }
}

int main() {
    Graph G=CreateGraph(DG);
    add_arc(G,'a','b',10);
    add_arc(G,'b','c',20);
    add_arc(G,'c','a',30);
    print_Graph(G);
    add_arc(G,'a','d',40);
    add_arc(G,'c','d',50);
    print_Graph(G);
    freeGraph(G);
    return 0;
}
